<?php
namespace app\common\component\help_get_rich;

use app\common\model\HelpToRich;
use app\common\model\HelpGetRich;

class HelpGetRichService {
	public function create($data) {
		$data['order_id'] = 'GET' . date('YmdHis') . rand(100, 999);
		$HelpGetRich = new HelpGetRich($data);

    	if($HelpGetRich -> save()) {
    		return [
    			'code' => 0,
    			'message' => '富豪区提现创建成功!',
    			'data' => [
    				'help_get_rich' => $HelpGetRich
    			]
    		];
    	}

    	return [
			'code' => -1,
			'message' => '富豪区提现创建失败!'
		];
	}

	public function withdraw($price) {
		$userId = \think\Session::get('user_id');
		$data['help_to_rich_id'] = 0;
    	$data['user_id'] = $userId;
    	$data['price'] = $price;
    	$data['begin_time'] = $data['create_time'] = time();
    	$data['status'] = 0;
    	$getLastHelpGet = HelpGetRich::where('status = 0 AND user_id = ' . $userId) -> order('sort DESC') -> find();
    	if($getLastHelpGet) {
    		$data['sort'] = $getLastHelpGet -> sort + 1;
    	}

		return $this -> create($data);
	}

	public function confirm($help_get_rich_id) {
		$getHelpGetRich = HelpGetRich::get(['help_get_rich_id' => $help_get_rich_id]);

		if(!$getHelpGetRich) {
			return [
				'code' => -1,
				'message' => '订单不存在'
			];
		}

		$getHelpToRich = HelpToRich::get(['help_to_rich_id' => $getHelpGetRich -> getData('help_to_rich_id')]);
		if(!$getHelpToRich) {
			return [
				'code' => -1,
				'message' => '订单不存在'
			];
		}
		$currentTime = time();

		try {
			$getHelpGetRich -> begin_time = $currentTime;
			$getHelpGetRich -> status = 4;
			if(!$getHelpGetRich -> save()) {
				throw new \Exception('确认收款失败');
			}

			$getHelpToRich -> begin_time = $currentTime;
			$getHelpToRich -> over_time = $currentTime + getParam('HELP_TO_RICH_PRICE_FREEZE_TIME') * 60 * 60;
			$getHelpToRich -> status = 4;
			if(!$getHelpToRich -> save()) {
				throw new \Exception('确认收款失败');
			}

			$remainPrice = $getHelpToRich -> price + (getParam('HELP_TO_RICH_PRICE_INCOME') / 100 * $getHelpToRich -> price);

			$freezeOperate = [];
			for($i=1;$i<=5;$i++) {
				if(!isset($freezeOperate[$i])) {
					$freezeOperate[$i] = [];
				}

				$createTime = $currentTime + ($i-1) * 86400;
				$endTime = $currentTime + $i * 86400;
				$freezeOperate[$i] = [
					'price' => $remainPrice / 5,
					'create_time' => $createTime,
					'end_time' => $endTime,
					'is_draw' => 0
				];
			}

			$createUserWalletRichFreeze = (new \app\common\component\user_wallet_rich_freeze\UserWalletRichFreezeService()) -> create([
				'user_id' => $getHelpToRich -> getData('user_id'),
				'price' => $getHelpToRich -> price,
				'interest' => getParam('HELP_TO_RICH_PRICE_INCOME') / 100 * $getHelpToRich -> price,
				'remain_price' => $remainPrice,
				'create_time' => $currentTime,
				'begin_time' => $currentTime,
				'freeze_operate' => json_encode($freezeOperate),
				//'end_time' => $getHelpToRich -> over_time,
				'end_time' => $currentTime + 432000,
				'remark' => '冻结打款帮助的本金与利息',
				'is_freeze' => 0
			]);
			if($createUserWalletRichFreeze['code'] !== 0) {
				throw new \Exception($createUserWalletRichFreeze['message']);
			}

			$createUserWalletDynamic = (new \app\common\component\user_wallet_dynamic\UserWalletDynamicService()) -> reward($getHelpToRich -> getData('user_id'), $getHelpToRich -> price);
			if($createUserWalletDynamic['code'] !== 0) {
				throw new \Exception($createUserWalletDynamic['message']);
			}
		} catch(\Exception $e) {
			return [
				'code' => -1,
				'message' => $e -> getMessage()
			];
		}

		return [
			'code' => 0,
			'message' => '确认收款成功'
		];
	}
}
?>